import {Environment} from 'framework/Environment'
import Session from 'framework/Session'

class AccountEnv extends Environment {
  isGuest() {
    return this.state.isGuest;
  }

  isAccess(access) {
    access = new Set(access);
    let intersectionSet = new Set([...this.state.account.rights].filter(x => access.has(x)));
    return intersectionSet.size > 0;
  }

}

export default new AccountEnv({
  name: 'account',
  state: {
    isGuest: true,
    account: null,
    token: null,
  },
  mutations: {
    account(state, account) {
      state.isGuest = false;
      state.account = account;
      state.token = account.token;
      Session.set('account', {
        account: account,
        token: account.token,
        guid: account.guid
      });
    },
    clear(state) {
      //state.isGuest = true;
      //state.account = null;
      //state.token = null;
      //Session.del('account');
    }
  }
});
